package com.framework.tool;

import com.framework.base.BaseDTO;
import com.framework.base.PageList;
import com.framework.common.constants.PshcpConstants;
import com.framework.exception.BizException;
import com.framework.util.MessageUtil;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @Description: TODO
 * @Author: xiaoche
 * @Date: 2022/10/29 17:32
 */
public class CommonTool {

    public static void checkString(String str, String errCode) throws BizException {
        if (StringUtils.isBlank(str)) {
            String errMsg = MessageUtil.getMessage(errCode);
            throw new BizException(errCode, errMsg);
        }
    }

    public static void checkBoolean(Boolean flag, String errCode) throws BizException {
        if (!flag) {
            String errMsg = MessageUtil.getMessage(errCode);
            throw new BizException(errCode, errMsg);
        }
    }

    public static void checkObj(Object obj, String errCode) throws BizException {
        if (ObjectUtils.isEmpty(obj)) {
            String errMsg = MessageUtil.getMessage(errCode);
            throw new BizException(errCode, errMsg);
        }
    }

    public static void checkExistObj(Object obj, String errCode) throws BizException {
        if (obj != null) {
            String errMsg = MessageUtil.getMessage(errCode);
            throw new BizException(errCode, errMsg);
        }
    }

    public static void throwBizException(String errCode) throws BizException {
        String errMsg = MessageUtil.getMessage(errCode);
        throw new BizException(errCode, errMsg);
    }

    public static void throwBizException(String errCode, String errMsg) throws BizException {
        throw new BizException(errCode, errMsg);
    }

    public static <T> T newTclass(Class<T> clazz) throws InstantiationException, IllegalAccessException {
        T t = clazz.newInstance();
        return t;

    }

    public static <T, R> T copyProperties(R source, Class<T> clazz) {
        if (source == null) {
            return null;
        }
        T target = null;
        try {
            target = newTclass(clazz);
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        BeanUtils.copyProperties(source, target);
        return target;
    }

    public static <T, R> void copyProperties(R source, T target) {
        if (source == null || target == null) {
            return;
        }
        BeanUtils.copyProperties(source, target);
    }

    public static <T, R> List<T> copyPropertiesList(List<R> source, Class<T> clazz) {
        if (CollectionUtils.isEmpty(source)) {
            return Collections.emptyList();
        }
        List<T> targets = new ArrayList<>();
        for (R s : source) {
            T target = copyProperties(s, clazz);
            targets.add(target);
        }
        return targets;
    }

    public static <T, R> PageList<T> buildPageList(PageInfo<R> logDOPageInfo, Class<T> clazz) {
        if (logDOPageInfo == null) {
            return null;
        }
        List<R> list = logDOPageInfo.getList();
        PageList<T> pageList = new PageList();
        List<T> tList = CommonTool.copyPropertiesList(list, clazz);
        pageList.setPageNum(logDOPageInfo.getPageNum());
        pageList.setPageSize(logDOPageInfo.getPageSize());
        pageList.setTotal(logDOPageInfo.getTotal());
        pageList.setPages(logDOPageInfo.getPages());
        pageList.setItems(tList);
        return pageList;
    }

    public static <T, R> PageList<T> buildPageList(PageList<R> pageList, Class<T> clazz) {
        if (pageList == null) {
            return new PageList<>();
        }
        List<R> list = pageList.getItems();
        PageList<T> tPageList = new PageList();
        List<T> tList = CommonTool.copyPropertiesList(list, clazz);
        tPageList.setPageNum(pageList.getPageNum());
        tPageList.setPageSize(pageList.getPageSize());
        tPageList.setTotal(pageList.getTotal());
        tPageList.setPages(pageList.getPages());
        tPageList.setItems(tList);
        return tPageList;
    }

    public static <T> PageList<T> buildPageList(List<T> list) {
        PageInfo<T> pageInfo = new PageInfo<>(list);
        PageList<T> pageList = new PageList();
        pageList.setPageNum(pageInfo.getPageNum());
        pageList.setPageSize(pageInfo.getPageSize());
        pageList.setTotal(pageInfo.getTotal());
        pageList.setPages(pageInfo.getPages());
        pageList.setItems(pageInfo.getList());
        return pageList;
    }

    public static <T> PageList<T> buildPageList(Integer pageNum, Integer pageSize, List<T> list, Long count) {
        long pages = count / pageSize;
        Integer ipages = 0;
        if (count % pageSize == 0) {
            ipages = (int) pages;
        } else {
            ipages = ((int) pages) + 1;
        }
        Integer icount = count == null ? 0 : (int) (count / 1);
        PageList<T> pageList = new PageList();
        pageList.setPageNum(pageNum);
        pageList.setPageSize(pageSize);
        pageList.setTotal(icount);
        pageList.setPages(ipages);
        pageList.setItems(list);
        return pageList;
    }

    public static BaseDTO buildBaseDTO(Integer pageNum, Integer pageSize) {
        BaseDTO baseDTO = new BaseDTO();
        baseDTO.setPageNum(pageNum == null || pageNum <= 0 ? PshcpConstants.PAGE_NUM : pageNum);
        baseDTO.setPageSize(pageSize == null || pageSize <= 0 ? PshcpConstants.PAGE_SIZE : pageSize);
        baseDTO.setPageSize(pageSize > PshcpConstants.MAX_PAGE_SIZE ? PshcpConstants.MAX_PAGE_SIZE : pageSize);
        return baseDTO;
    }

    // 姓名脱敏
    public static String getNameResult(String name){
        if(StringUtils.isEmpty(name)){
            return null;
        }
        String resultName = null;
        if (name.length() == 1) {
            resultName = name;
        }else if (name.length() == 2) {
            resultName = name.replaceFirst(name.substring(1), "*");
        }else if (name.length() > 2) {
            resultName = name.replaceFirst(name.substring(1, name.length() - 1), "*");
        }
        return resultName;
    }
}
